package recycle.impl;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import recycle.dao.HotcommentMapper;
import recycle.dao.ProductcommentMapper;
import recycle.pojo.Hotcomment;
import recycle.pojo.Productcomment;
import recycle.service.HotcommentService;
import recycle.utils.Page;
import recycle.vo.HotcommentParam;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
* 描述：热门商品评论表 服务实现层接口
* @author 孙冉
* @date 2019/07/22
*/
@Service
public class HotcommentServiceImpl implements HotcommentService{
	@Resource
	private HotcommentMapper hotcommentMapper;
	@Resource
	private ProductcommentMapper productcommentMapper;
 	
 	/**
    * 描述：根据Id获取Hotcomment对象
    * @param id
    */
    public Hotcomment findById(Serializable hotCommentId){
    	return hotcommentMapper.findById(hotCommentId);
    }
    /**
    * 描述：获取所有的Hotcomment对象
    */
    public List<Hotcomment> findAll(){
    	return hotcommentMapper.findAll();
    }

	/**
    * 描述：新增保存Hotcomment对象
    */
    public int save(Hotcomment hotcomment){
    	return  hotcommentMapper.save(hotcomment);
    }

	/**
    * 描述：根据Id删除Hotcomment对象
    * @param id
    */
    public int delete(Serializable hotCommentId){
    	return  hotcommentMapper.delete(hotCommentId);
    }
	
	/**
    * 描述：更新Hotcomment对象
    */
    public int update(Hotcomment hotcomment){
    	return  hotcommentMapper.update(hotcomment);
    }
    
    /**
     * 根据不确定的条件，动态查询
     */
    public List<Hotcomment> query(HotcommentParam param){
    	return  hotcommentMapper.query(param);
    }
    
    /**
     * 根据不确定的条件，动态分页查询
     */
    public Page queryByPage(HotcommentParam param){
    	Page page = new Page();
    	if(param.isPage()){
			int totalCount = hotcommentMapper.getCount(param);
			int totalPageCount = (totalCount-1)/page.getPageSize()+1;
			if(param.getPageIndex()<1){
				param.setPageIndex(1);
			}else if(param.getPageIndex()>totalPageCount){
				param.setPageIndex(totalPageCount);
			}
			List list = hotcommentMapper.query(param);

			page.setTotalCount(totalCount);
			page.setTotalPageCount(totalPageCount);
    		page.setPageNo(param.getPageIndex());
    		page.setPageSize(param.getSize());
			page.setList(list);
		}
    	return page;
    	
    }

	/**
	 * 定时更新热门评论
	 * @return
	 */
	@Override
	@Scheduled(cron = "00 00 00 * * *")
	public List<Hotcomment> timingUpdateHotcomment() {
		List<Productcomment> hotcommentsList =   productcommentMapper.selectProductcommentOrderByCreateDate();
		for(int i=0;i<hotcommentsList.size();i++){
			Productcomment pc = new Productcomment();
			Hotcomment hc = new Hotcomment();
			hc.setHotCommentId(pc.getCommentId());
			hc.setAuditStatus(pc.getAuditStatus());
			hc.setAuditTime(pc.getAuditTime());
			hc.setContent(pc.getContent());
			hc.setCustomerId(pc.getCustomerId());
			hc.setModifiedTime(pc.getModifiedTime());
			hc.setOrderId(pc.getOrderId());
			hc.setOther1(pc.getOther1());
			hc.setOther2(pc.getOther2());
			hc.setProductId(pc.getProductId());
			hc.setTitle(pc.getTitle());
			hotcommentMapper.save(hc);
		}
		return hotcommentMapper.findAll();
	}
}
